package com.wty.basic.reflect;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author wangtengyu
 * @Create 2018-08-09-9:55
 */
public class constructorDemo {



    @Test
    public void testNewInstance() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class clazz=Class.forName("com.wty.basic.reflect.Student");
        //无参构造不能为空
        Student student=(Student) clazz.newInstance();
        System.out.println(student.toString());
    }

    @Test
    public void testNewInstance2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class clazz=Class.forName("com.wty.basic.reflect.Student");
        Constructor con=clazz.getDeclaredConstructor(String.class,int.class);

//        Constructor constructors[]=clazz.getConstructors();//仅仅能获得公有的
//        for (Constructor constructor:constructors){
//            System.out.println(constructor);
//        }
        con.setAccessible(true);
        System.out.println(con);
        Student student= (Student) con.newInstance("wangtengyu",15);
        System.out.println(student);
    }




}
